
package cloning.cci4.ch1;

/*
 * Write a method to decide if two strings are anagrams or not.
 */
public class Task_1_4
{

	public static void main(String[] args)
	{
		String s = "abc";
		String t = "abcabc";
		String t1 = "aabbcc";
		
		System.out.println("source:" + s + " target:" + t + " result=" + anagram(s, t));
		System.out.println("source:" + s + " target:" + t1 + " result=" + anagram(s, t1));
		System.out.println("source:" + t + " target:" + t1 + " result=" + anagram(t, t1));
		
	}

	private static boolean anagram(String s, String t) {
		
		if (s.length() != t.length())
		{
			return false;
		}
		
		char[] sourceArray = s.toCharArray();
		char[] targetArray = t.toCharArray();
		
		for (int sourceIndex = 0; sourceIndex < sourceArray.length; sourceIndex++)
		{
			char sourceChar = sourceArray[sourceIndex];
			int sourceCharCount = 0;
			
			for (int innerIndex = 0; innerIndex < sourceArray.length; innerIndex++)
			{
				if (sourceChar == sourceArray[innerIndex])
				{
					sourceCharCount++;
				}
			}
			
			int targetCharCount = 0;
			
			for (int targetIndex = 0; targetIndex < targetArray.length; targetIndex++)
			{
				if (sourceChar == targetArray[targetIndex])
				{
					targetCharCount++;
				}
			}
			
			if (sourceCharCount != targetCharCount)
			{
				return false;
			}
		}
		
		
		return true;
	}


	
}
